uniform sampler2D tex;
uniform vec3 posCamera;

varying vec4 pos;

float randomf(vec3 vector) { // in [0.0, 1.0]
	return fract(sin(dot(vector.xyz, vec3(12.9898, 78.233, 18.78765))) * 43758.5453);
}

void main() {
	vec4 texel = texture2D(tex, gl_TexCoord[0].st);
	float dist = sqrt(distance(posCamera, pos.xyz) * 0.01); // in [0.0, ~1.0]
	float lightmod = clamp(1.0 - dist, 0.0, 1.0);
	float randf = randomf(pos.xyz);
	randf = 1.0 - randf * 0.07; // in [0.93, 1.0]
	float texelmod = randf * lightmod;
	texel.r *= texelmod;
	texel.g *= texelmod;
	texel.b *= texelmod;
	gl_FragColor = texel;
}